import time
from pyecharts.charts import Line
import akshare as ak
import pyecharts.options as opts
from pyecharts.globals import ThemeType
from typing import List

symbolList = ['上证指数', '全指消费', '全指可选', '全指医药', '全指金融地产','全指信息', '全指通信', '全指能源', '全指材料', '全指工业', '全指公用']
indicatorList = ['市盈率', '市净率']

# 处理指数估值数据
def handle_valuation_his_chart(symbolList: List[str], indicator: str) -> None:
    chartXData, chartYData = [], [] # X,Y轴数据

    # 获取数据
    for e in symbolList:
        df = ak.index_value_hist_funddb(symbol=e, indicator=indicator)
        chartXData = df['日期'].tolist()
        chartYData.append(df[indicator].tolist())
        print(f'{e} - {indicator} - 数据获取完成')

    # 绘制图表 - 折线图
    chart = Line(
        # 设置主题、画布宽度、高度等信息
        init_opts=opts.InitOpts(theme=ThemeType.DARK, width="1400px", height="600px"),
    )

    # 全局设置
    chart.set_global_opts(
        title_opts=opts.TitleOpts(title=f'指数估值-{indicator}'),
        # 工具箱配置
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        # 区域缩放配置
        datazoom_opts=[opts.DataZoomOpts(is_show=True, range_start=0, range_end=100)],
    )

    chart.add_xaxis(xaxis_data=chartXData)  # 添加x轴数据
    for i, d in enumerate(chartYData):
        chart.add_yaxis(
            series_name=symbolList[i],
            y_axis=d,  # y轴数据
            is_connect_nones=True,  # 连接空数据
            markpoint_opts=opts.MarkPointOpts(
                data=[opts.MarkPointItem(type_="max"), opts.MarkPointItem(type_="min")],
            ),
        )
    date = time.strftime('%Y%m%d', time.localtime(time.time()))
    chart.render(f"指数估值-{indicator}-{date}.html")


for e in indicatorList:
    handle_valuation_his_chart(symbolList, e)
